package com.xueyi.system.signin.controller;

import com.xueyi.common.security.utils.SecurityUtils;
import com.xueyi.system.api.organize.domain.dto.SysDeptDto;
import com.xueyi.system.api.organize.domain.dto.SysUserDto;
import com.xueyi.system.organize.service.ISysDeptService;
import com.xueyi.system.signin.domain.query.SigninQuery;
import com.xueyi.system.signin.domain.dto.SigninDto;
import com.xueyi.system.signin.service.ISigninService;
import com.xueyi.common.web.entity.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 签到管理 业务处理
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/signin")
public class SigninController extends BaseController<SigninQuery, SigninDto, ISigninService> {




    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISigninService signinService;

    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "签到" ;
    }

    /**
     * 查询签到列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.SIGNIN_LIST)
    public AjaxResult list(SigninQuery signin) {
        return super.list(signin);
    }

    /**
     * 查询签到详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.SIGNIN_SINGLE)
    public AjaxResult getInfoExtra(@PathVariable Serializable id) {
        return super.getInfoExtra(id);
    }

    /**
     * 签到导出
     */
    @Override
    @PostMapping("/export")
    @RequiresPermissions(Auth.SIGNIN_EXPORT)
    @Log(title = "签到管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, SigninQuery signin) {
        super.export(response, signin);
    }

    /**
     * 签到新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.SIGNIN_ADD)
    @Log(title = "签到管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody SigninDto signin) {
        return super.add(signin);
    }

    /**
     * 签到修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.SIGNIN_EDIT)
    @Log(title = "签到管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody SigninDto signin) {
        return super.edit(signin);
    }

    /**
     * 签到批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.SIGNIN_DEL)
    @Log(title = "签到管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    /**
     * 获取签到选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }




    /**
     * 签到新增
     */
    @PostMapping("/addSigninDto")
    @Log(title = "签到管理", businessType = BusinessType.INSERT)
    public AjaxResult addSigninDto() {
        SigninDto signin = new SigninDto();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SysUserDto user = SecurityUtils.getUser();
        signin.setUserId(user.getId()+"");
        signin.setUserName(user.getUserName());
        if(user.getDeptId() != null){
            signin.setDeptId(user.getDeptId().toString());
            SysDeptDto deptDto = deptService.selectById(user.getDeptId());
            signin.setDeptName(deptDto.getName());
        }
        signin.setSignInDate(dateFormat.format(new Date()));
        return super.add(signin);
    }


    /**
     * 查询签到列表
     */
    @GetMapping("/getNowDAte")
    @RequiresPermissions(Auth.SIGNIN_LIST)
    public AjaxResult getNowDAte() {
        SysUserDto user = SecurityUtils.getUser();
        List<SigninDto> dtoList = signinService.getNowDAte(user.getId());
        if(dtoList.size() == 0){
            return new AjaxResult().put("status",false);
        }else {
            return new AjaxResult().put("status",true);
        }
    }

    interface Auth {
        /** 系统 - 签到管理 - 列表 */
        String SIGNIN_LIST = "system:signin:list" ;
        /** 系统 - 签到管理 - 详情 */
        String SIGNIN_SINGLE = "system:signin:single" ;
        /** 系统 - 签到管理 - 新增 */
        String SIGNIN_ADD = "system:signin:add" ;
        /** 系统 - 签到管理 - 修改 */
        String SIGNIN_EDIT = "system:signin:edit" ;
        /** 系统 - 签到管理 - 删除 */
        String SIGNIN_DEL = "system:signin:delete" ;
        /** 系统 - 签到管理 - 导出 */
        String SIGNIN_EXPORT = "system:signin:export" ;
    }
}
